<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');


/**
 * This class is for online shop order process - payment.
 * params order step
 * This is the children of controller.
 * PHP5 CMS Typolight
 * Copyright is 34n.sam@gmail.com
 */

class GetPaymentCurrency extends Controller
{
	/**
	 * This is class construct function.
	 * @params null
	 * @Return null
	 */
	public function __construct()
	{
		parent::__construct();		
		header('Cache-Control: no-store, no-cache, must-revalidate');
	}
	
	public function run()
	{
		$this->import('Database');
		if (!$_COOKIE['PAY_CURRENCY']){
			$objSession = $this->Database->prepare("SELECT * FROM tl_session WHERE hash=? AND name=?")
										->execute($_COOKIE['FE_USER_AUTH'], 'FE_USER_AUTH');
			$obj_member = $this->Database->prepare("SELECT * FROM tl_member WHERE id=?")
										->execute($objSession->pid);
			$preferred = ($obj_member->cur)?$obj_member->cur:$_COOKIE['DAFT_CURRENCY'];
			if (!$preferred){
				$obj_currency = $this->Database->prepare("SELECT * FROM `tl_currency` WHERE `is_default`=1")
												->execute(time());
				
				$preferred = $obj_currency->iso_code;
			}
			
			setcookie("PAY_CURRENCY", $preferred);
		}
		
		$obj_currency = $this->Database->prepare("SELECT * FROM `tl_currency`")
										->execute(time());
		$arr_currency = array();
		while($obj_currency->next()){
			$arr_currency[$obj_currency->iso_code] = $obj_currency->iso_code;
		}
		$select_currency = '<select id="payment_select_cur" name="cur">';
		foreach ($arr_currency as $key=>$currency){
			$select_currency .= '<option value="'.$key.'"'.(($_COOKIE['PAY_CURRENCY']==$key||(!$_COOKIE['PAY_CURRENCY']&&$preferred==$key))?' selected="selected"':'').'>'.$currency.'</option>';
		}
		$select_currency .= "</select>";
		
		$str_html = '<script type="text/javascript">					
						jQuery(function(){
							jQuery("#payment_select_cur").change(function(el){
								var _target = $(el.target)||$(el);
								$.cookie("PAY_CURRENCY",_target.val());	
								$.ajax({
									url:"change_currency.php",
									type:"post",
									data:"cur="+_target.val(),
									success:function(res){
										jQuery("#payment_method_area").html(res);
									}
								});
							});
						});
					</script>';
		$str_html .= "<span class=\"currency\">Select your preferred currency: ".$select_currency."</span>";
		echo $str_html;
	}
}

$payment_currency = new GetPaymentCurrency();
$payment_currency->run();